package cz.byteworks.android.myway.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import cz.byteworks.android.myway.activities.R;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.pojo.Way;

public class DataArrayAdapter<T> extends ArrayAdapter<T> {

	List<T> list;
	Context ctx;
	LayoutInflater infl;
	DataHelper dh;
	int actualWayId;
	
	public DataArrayAdapter(Context context, int resource, List<T> list, LayoutInflater layoutInflater) {
		super(context, resource, list);
		this.list = list;
		ctx = context;
		this.infl = layoutInflater;
		dh = new DataHelper(ctx);
		actualWayId = dh.getActualWay();
	}
	
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout ll = (LinearLayout) infl.inflate(R.layout.data_item, parent, false);
		Way way = (Way) list.get(position);
		
		
		if (way.getId() == actualWayId) {
			ll.setBackgroundColor(Color.argb(100, 0, 68, 0));
		}
		ImageView image = (ImageView) ll.findViewById(R.id.dataImage);
		image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.compass));
		TableLayout tbl = (TableLayout) ll.findViewById(R.id.dataTable);
		TableRow tr1 = (TableRow) tbl.findViewById(R.id.dataTableRow1);
		TableRow tr2 = (TableRow) tbl.findViewById(R.id.dataTableRow2);
		TextView tv1 = (TextView) tr1.findViewById(R.id.dataText1);
		TextView tv2 = (TextView) tr2.findViewById(R.id.dataText2);
		tv1.setText(way.getName());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		String info = "Created: "+sdf.format(way.getCreateTime());
		if (way.getId() == actualWayId) {
			info = "Actual Way | "+info;
		}
		tv2.setText(info);
		return ll;
	}
	
	

}
